package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;
    
    /**
     * 新增用户
     * @param user 用户信息
     * @return 结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> save(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userService.save(user);
            if (success) {
                result.put("code", 200);
                result.put("message", "新增用户成功");
                result.put("data", user);
            } else {
                result.put("code", 400);
                result.put("message", "用户名已存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "新增用户失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
    
    /**
     * 更新用户
     * @param id 用户ID
     * @param user 用户信息
     * @return 结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> update(@PathVariable Integer id, @RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            user.setId(id);
            boolean success = userService.update(user);
            if (success) {
                result.put("code", 200);
                result.put("message", "更新用户成功");
                result.put("data", user);
            } else {
                result.put("code", 400);
                result.put("message", "用户不存在或用户名已被占用");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "更新用户失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
    
    /**
     * 删除用户
     * @param id 用户ID
     * @return 结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userService.deleteById(id);
            if (success) {
                result.put("code", 200);
                result.put("message", "删除用户成功");
            } else {
                result.put("code", 400);
                result.put("message", "用户不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除用户失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
    
    /**
     * 批量删除用户
     * @param ids ID列表
     * @return 结果
     */
    @DeleteMapping("/batch")
    public ResponseEntity<Map<String, Object>> deleteBatch(@RequestBody List<Integer> ids) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userService.deleteBatch(ids);
            if (success) {
                result.put("code", 200);
                result.put("message", "批量删除用户成功");
            } else {
                result.put("code", 400);
                result.put("message", "批量删除用户失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "批量删除用户失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
    
    /**
     * 查询所有用户
     * @return 用户列表
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> findAll() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<User> list = userService.findAll();
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", list);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
    
    /**
     * 根据ID查询用户
     * @param id 用户ID
     * @return 用户信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> findById(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            User user = userService.findById(id);
            if (user != null) {
                result.put("code", 200);
                result.put("message", "查询成功");
                result.put("data", user);
            } else {
                result.put("code", 400);
                result.put("message", "用户不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
    
    /**
     * 条件查询
     * @param params 查询条件
     * @return 用户列表
     */
    @GetMapping("/condition")
    public ResponseEntity<Map<String, Object>> findByCondition(@RequestParam Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<User> list = userService.findByCondition(params);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", list);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
    
    /**
     * 模糊查询
     * @param keyword 关键字
     * @return 用户列表
     */
    @GetMapping("/search")
    public ResponseEntity<Map<String, Object>> findByKeyword(@RequestParam String keyword) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<User> list = userService.findByKeyword(keyword);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", list);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
    
    /**
     * 分页查询
     * @param params 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public ResponseEntity<Map<String, Object>> findPage(@RequestParam Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            Map<String, Object> page = userService.findPage(params);
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", page);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
}